﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Collections.ObjectModel;
using RiskMan.DataClass.Models.Plaza_II;
using System.Reflection;

namespace RiskMan.Config
{
    public class XmlHistory
    {
     
        public static string FilenameWindow = "\\Window.xml";
        public static string FilenamePlazaII = "\\PlazaRobot.xml";

        public static object GetXmlData(string filename)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\PlazaII";

            if (!Directory.Exists(path))
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                dir.Create();
            }

            if (filename == FilenameWindow)
            {
                try
                {
                    XmlSerializer reader = new XmlSerializer(typeof(List<string>));
                    StreamReader file = new StreamReader(path + filename);
                    List<string> result = new List<string>();
                    result = (List<string>)reader.Deserialize(file);
                    return result;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }

           
         
            if (filename == FilenamePlazaII)
            {
                try
                {
                    var reader = new XmlSerializer(typeof(List<RobotItemPlazaII>));
                    StreamReader file = new StreamReader(path + filename);
                    List<RobotItemPlazaII> result = new List<RobotItemPlazaII>();
                    result = (List<RobotItemPlazaII>)reader.Deserialize(file);

                    ObservableCollection<RobotItemPlazaII> res = new ObservableCollection<RobotItemPlazaII>();
                    for (int i = 0; i < result.Count; i++)
                    {
                        result[i].IsRun = false;
                        result[i].GuidItem = Guid.NewGuid();
                        res.Add(result[i]);
                    }

                    return res;

                }
                catch (Exception)
                {
                    return new ObservableCollection<RobotItemPlazaII>();
                }
            }
       

          
            return null;
        }

        public static void SetXmlData(string filename, object List)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\PlazaII";

            if (!Directory.Exists(path))
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
                dir.Create();
            }


            if (filename == FilenameWindow)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<string>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    Cfg.SetLog(ex.Message, new XmlHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }


           if (filename == FilenamePlazaII)
            {
                try
                {
                    XmlSerializer write = new System.Xml.Serialization.XmlSerializer(typeof(List<RobotItemPlazaII>));
                    using (StreamWriter file = new System.IO.StreamWriter(path + filename))
                        write.Serialize(file, List);
                }
                catch (Exception ex)
                {
                    Cfg.SetLog(ex.ToString(), new XmlHistory().GetType(), MethodBase.GetCurrentMethod());
                }
            }
         
        }
    }
}